<script setup lang="ts">
// //
import CustomNavBar from '@/pages/index/components/CustomNavBar.vue'
import CategoryPanel from '@/pages/index/components/CategoryPanel.vue'
import HotPanel from '@/pages/index/components/HotPanel.vue'
// import { getHomeBannerApi, getHomeCategoryApi, getHomeHotAPI } from '@/services/home/home'
import { onLoad } from '@dcloudio/uni-app'
import { ref, watch } from 'vue'
import { useMemberStore } from '@/stores'
import PageSkeleton from '@/pages/index/components/PageSkeleton.vue'
import {getPayProductsPage} from "@/api/products";
import type {PayProductsData, PayProductsRespPageData} from "@/api/products/types";

import TtscGuess from "@/components/TtscGuess.vue";
import {useGuessList} from "@/components";


const categoryList = ref<PayProductsData[]>()

// 获取猜你喜欢组件实例
const { guessRef, onScrollToLower } = useGuessList()
const isTriggered = ref(false)
const isLoading = ref(true)


const getHomeCategoryData = async () => {
  const res = await getPayProductsPage({pageSize: 6, pageNo: 1})
  categoryList.value = res.data.list
}


// 滚动触底事件
// const onScrollToLower = async () => {
//   await Promise.all([guessRef.value?.getMore()])
// }

//下拉刷新
const refReSherRefresh = async () => {
  isTriggered.value = true
  // 先重置子组件数据
  guessRef.value?.resetData()
  // 刷新数据
  await Promise.all([getHomeCategoryData()])
  // 重新加载
  guessRef.value?.getMore()
  isTriggered.value = false
}

onLoad(async () => {
  isLoading.value = true
  await Promise.all([getHomeCategoryData()])
  isLoading.value = false
})

const memberStore = useMemberStore()

// 新增token变化监听
watch(
  () => memberStore.profile,
  (newVal) => {
    if (newVal) {
      getHomeCategoryData()
    }
  }
)

</script>

<template>
  <!--  导航栏-->
  <CustomNavBar />
  <scroll-view
    scroll-y="true"
    class="scroll"
    refresher-enabled="true"
    :refresher-triggered="isTriggered"
    @scrolltolower="onScrollToLower"
    @refresherrefresh="refReSherRefresh"
  >
    <PageSkeleton v-if="isLoading" />
    <template v-else>
      <!-- 轮播图 -->
<!--      <TtscSwiper :bannerList="bannerList" />-->
      <!-- 分类面板 -->
      <CategoryPanel :categoryList="categoryList" />
      <!-- 热门推荐 -->
<!--      <HotPanel :hotList="categoryList" />-->
      <!-- 猜你喜欢 -->
      <TtscGuess ref="guessRef" />
    </template>
  </scroll-view>
</template>

<style scoped lang="scss">
page {
  background-color: #f7f7f7;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.scroll {
  flex: 1;
}
</style>
